package Codigo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.SignatureException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;

public class DigestCalculator {
	
	public static String decodeChavePrivada(String fraseSecreta, String texto){

		try {
			byte[] bufferKey = fraseSecreta.getBytes("UTF8");

			KeyGenerator keyGen;

			keyGen = KeyGenerator.getInstance("DES");
			Key key = keyGen.generateKey();
		    
			Cipher cipher = Cipher.getInstance("DES/ECB/PKCS8Padding");

			
			cipher.init(Cipher.DECRYPT_MODE, key);
			byte[] newPlainText = cipher.doFinal(texto.getBytes());
			
			return newPlainText.toString();

		}catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    	
    }
	public String getKeyPrivate(String texto, String chave){
		try {
	            KeyPairGenerator keygen = KeyPairGenerator.getInstance("SHA-1");
	            SecureRandom secrand = new SecureRandom();
	            secrand.setSeed(chave.getBytes());
	            keygen.initialize(512, secrand);
	            KeyPair keys = keygen.generateKeyPair();

	            PrivateKey prikey = keys.getPrivate();

	            java.security.Signature signet = java.security.Signature.getInstance("SHA-1");
	            signet.initSign(prikey);
	            signet.update(texto.getBytes());
	            byte[] signed = signet.sign();
	            return signed.toString();

	        } catch (NoSuchAlgorithmException e) {
	            System.out.println("NoSuchAlgorithmException");
	        } catch (InvalidKeyException e) {
	            System.out.println("InvalidKeyException");
	        } catch (SignatureException e) {
	            System.out.println("SignatureException");
	        } catch (Exception e) {
	            System.out.println("Exception");
	        }		

		return null;
	}
	public static String criptSHA1(String senha){

		try {
			byte[] plainText = senha.getBytes("UTF8");
			KeyPairGenerator keyGen;
			
			keyGen = KeyPairGenerator.getInstance("SHA-1");
			keyGen.initialize(512);
			KeyPair key = keyGen.generateKeyPair();
			
			Signature sig = Signature.getInstance("SHA-1");
			
			sig.initSign(key.getPrivate());
			sig.update(plainText);
			
			byte[] signature = sig.sign();
			
			StringBuffer buf = new StringBuffer();
			for(int i = 0; i < signature.length; i++) {
			String hex = Integer.toHexString(0x0100 + (signature[i] & 0x00FF)).substring(1);
			buf.append((hex.length() < 2 ? "0" : "") + hex);
			}
			return buf.toString();

		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SignatureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public static void cripFrase(String fileName, String fraseSecreta)
	{
        try
        {

        	byte[] buffer = fraseSecreta.getBytes("UTF8");  
	        
        	KeyGenerator keyGen = KeyGenerator.getInstance("DES");
        	
        	keyGen.init(56);
        	
        	Key key = keyGen.generateKey();
        	
        	Cipher cipher = Cipher.getInstance("DES/ECB/PKCS8Padding");

        	cipher.init(Cipher.ENCRYPT_MODE, key);
        	byte[] cipherText = cipher.doFinal(buffer);


        	StringBuffer buf = new StringBuffer();
        	
        	for(int i = 0; i < cipherText.length; i++) {
	        	String hex = Integer.toHexString(0x0100 + (cipherText[i] & 0x00FF)).substring(1);
	        	buf.append((hex.length() < 2 ? "0" : "") + hex);
        	}
        	gravarArqChave(buf.toString(), fileName);
	    }
        catch( Exception ex )
        {
        	ex.printStackTrace();
        }
        
	}

	private static void gravarArqChave(String string, String fileName) {
		try {  
			
			File arquivo;  
	  
	         arquivo = new File(fileName);  
	         FileOutputStream fos = new FileOutputStream(arquivo);  

	         fos.write(string.getBytes());
	      }  
	      catch (Exception ee) {  
	         ee.printStackTrace();  
	      }  
	}
}
